//
//  BKMainViewController.m
//  bestkeep
//
//  Created by 武探 on 2016/11/3.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMainViewController.h"
#import "BKMainCommon.h"
#import "BKMain_ClassifyKindsHeaderView.h"
#import "BKScrollPageView.h"
#import "BKFirstPageNewsListViewController.h"
#import "Mediator+BKSearch.h"
#import "Mediator+BKScan.h"
#import "SystemMessageVC.h"
#import "BKMain_EvaluateAlertView.h"
#import "BKMain_CustomSwitchBtn.h"

@interface BKMainViewController ()<BKScrollPageViewDelegate,BKMain_ClassifyKindsHeaderViewDelegate>
@property(nonatomic,strong)NSArray<UIViewController *> *childVcs;
@property(nonatomic,strong)UIView *badgeView;
@property(nonatomic,assign)BOOL isHaveMoreLayout;
@property(nonatomic,strong)UIWindow *window;
@property(nonatomic,strong)NSArray *titleArray;//标题数组
@property(nonatomic,strong)NSArray *pictures;
@property(nonatomic,strong)NSArray *select_pictures;
@property (nonatomic,strong) NSArray *categoryArray;
@property(nonatomic,strong)BKScrollPageView *scrollPageView;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,strong)BKMain_ClassifyKindsHeaderView *fourLayoutView;
@property(nonatomic,strong)ExceptionView *exceptionView;
@property(nonatomic,strong)UIView *backView;
@property(nonatomic,strong)UIView *titleView;
@property(nonatomic,strong)UIButton *rightBtn;
@property(nonatomic,strong)UIButton *leftBtn;

//@property(nonatomic,strong)BKMain_CustomSwitchBtn *changeShowTypeBtn;//转换cell布局的Btn
//@property(nonatomic,assign)NSInteger type_change;

@end

@implementation BKMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _backView = [[UIView alloc]initWithFrame:self.view.frame];
    _backView.backgroundColor = [UIColor clearColor];
    [self.view addSubview: _backView];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self setUpNavigation];
    [self showEvaluateAPPView];
//    self.type_change = 2;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadUI:) name:@"isRestNotification" object:@"isRest"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideSearch) name:@"HideNaviSearchNotion" object:@"BK_main_Hide"];
    [self requestTitleData];
    
}

-(void)hideSearch{
    self.titleView.hidden = YES;
    self.rightBtn.hidden = YES;
    self.leftBtn.hidden = YES;
    
}
-(void)reloadUI:(NSNotification *)sender{
    NSString *isRestValue = sender.userInfo[@"isRest"];
    if ([isRestValue isEqualToString:@"1"]) {
        self.fourLayoutView.isRest = YES;
    }else{
        [self.fourLayoutView recordSearchConditionWithSearchCondition:sender.userInfo[@"order_key"] order:sender.userInfo[@"sort_name"]];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
//    self.navigationController.navigationBar.translucent = NO;
    self.titleView.hidden = NO;
    self.leftBtn.hidden = NO;
    self.rightBtn.hidden = NO;
    [self isHaveNewSystemMessage];
   
    
}
-(void)viewWillDisappear:(BOOL)animated{
   self.navigationController.navigationBar.translucent = YES;
   
}


#pragma mark ------------------BKMain_ClassifyKindsHeaderViewDelegate-----------------
//按价格升降序
-(void)ClassifyKindsHeaderView:(BKMain_ClassifyKindsHeaderView *)headerView priceCompare:(NSString *)priceCompare{
    NSString *str = [NSString stringWithFormat:@"%ld",(long)self.index];
    NSDictionary *dic = @{@"currentPage":@"1",
                          @"order_key":@"price",
                          @"sort_name":priceCompare,
                          @"index": str
                          };
    [[NSNotificationCenter defaultCenter] postNotificationName:@"priceNotication" object:@"priceType" userInfo:dic];
}
//按请求的下标
-(void)ClassifyKindsHeaderView:(BKMain_ClassifyKindsHeaderView *)headerView index:(NSInteger)index{
    NSString *order_key;
    switch (index) {
        case 0:
            order_key = @"";

            break;
        case 1:
            order_key = @"saledCount";
            break;
        case 2:
            order_key = @"newGoods";
            break;
        case 3:
            order_key = @"price";
            break;
        default:
            break;
    }
    NSString *str = [NSString stringWithFormat:@"%ld",(long)self.index];
    NSDictionary *dic = @{@"currentPage":@"1",
                          @"order_key":order_key,
                          @"sort_name":@"",
                          @"index": str
                          };
    [[NSNotificationCenter defaultCenter]postNotificationName:@"typeNotication" object:@"classifyType" userInfo:dic];

}

#pragma mark ------------------设置导航栏--------------------------------
-(void)setUpNavigation{
    [self createItem];
    [self createSeachView];
    
}
-(void)createItem{
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn.frame = CGRectMake(0, 0, 23, 23);
    UIImageView *leftImage = [[UIImageView alloc]init];
//    UIImageView *leftImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BKMain_sweepIcon"]];
    leftImage.frame = self.leftBtn.frame;
    [self.leftBtn addSubview:leftImage];
    [self.leftBtn setImage:[UIImage imageNamed:@"BKMain_sweepIcon"] forState:UIControlStateNormal];
    [self.leftBtn addTarget:self action:@selector(sweepBtnAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftBtn];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    space.width = -4;
    self.navigationItem.leftBarButtonItems = @[space, leftItem];
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn.frame = CGRectMake(0, 0, 28, 23);
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BKMain_消息中心icon"]];
    imageView.frame = CGRectMake(5, 0, 23, 23);
    self.badgeView = [[UIView alloc]initWithFrame:CGRectMake(17, 0, 6, 6)];
    self.badgeView.layer.cornerRadius = 3;
    self.badgeView.layer.masksToBounds = YES;
    self.badgeView.backgroundColor = [UIColor redColor];
    [imageView addSubview:self.badgeView];
    [self.rightBtn addSubview:imageView];
    [self.rightBtn addTarget:self action:@selector(infoAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    

}
-(void)createSeachView{
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 98, 30)];
    self.titleView.layer.cornerRadius = 15;//3;
    self.titleView.layer.masksToBounds = YES;
    [self.titleView setBackgroundColor:[UIColor clearColor]];
    
    UIView *searchBarView = [[UIView alloc]initWithFrame:CGRectMake(4, 0, ScreenWidth-86, 30)];
    searchBarView.backgroundColor = RGBACOLOR(218, 218, 218, 0.5);
    searchBarView.layer.masksToBounds = YES;
    searchBarView.layer.cornerRadius = 15;
    [self.titleView addSubview: searchBarView];
    
    UITextField * searchTextField = [[UITextField alloc]initWithFrame:self.titleView.bounds];
    searchTextField.backgroundColor = [UIColor clearColor];// [UIColor colorWithRed:(218/255.0) green:(218/255.0) blue:(218/255.0) alpha:0.5];
    searchTextField.font = [UIFont systemFontOfSize:14];
    
    searchTextField.placeholder = @"  请输入商品名称";
    [searchTextField setValue:RGB(178, 178, 178) forKeyPath:@"_placeholderLabel.textColor"];
    searchTextField.textAlignment = NSTextAlignmentCenter;
    [searchBarView addSubview:searchTextField];
    UIImageView * imgV = [[UIImageView alloc]init];
    imgV.frame = CGRectMake(searchTextField.frame.size.width/2-10-[self sizeWithContentString:searchTextField.placeholder].width/2,8, 14, 14);
    imgV.image = [UIImage imageNamed:@"searchIcon"];
    [self.titleView addSubview:imgV];
    UIControl *gotoSeaarchControl = [[UIControl alloc] initWithFrame:searchTextField.bounds];
    [gotoSeaarchControl addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    [searchTextField addSubview:gotoSeaarchControl];
    self.navigationItem.titleView = self.titleView;

}

-(CGSize)sizeWithContentString:(NSString *)content{
    
    UIFont * font = [UIFont systemFontOfSize:14];
    CGSize  size = CGSizeMake(1000, 15);
    
    return  [self sizeWithContentString:content textFont:font size:size];
}

-(CGSize)sizeWithContentString:(NSString *)content textFont:(UIFont *)font size:(CGSize)contentSize
{
    
    NSDictionary * fontDict = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    CGRect rect = [content boundingRectWithSize:contentSize options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDict context:nil];
    
    return rect.size;
}

//扫一扫
-(void)sweepBtnAction{
    Mediator *mediator = [Mediator sharedInstance];
    UIViewController *scanVC = [mediator BKScanViewController];
    [self.navigationController pushViewController:scanVC animated:YES];
}
//查看系统消息
-(void)infoAction{
    if ([BKMainCommon islogin] == YES) {
        SystemMessageVC *systemMessage = [[SystemMessageVC alloc] init];
        [self.navigationController pushViewController:systemMessage animated:YES];
    } else {
        [[Mediator sharedInstance] userGoToLoginFromOrignalController:self finishBlock:^{
            
        }];
    }
 
}
//搜索跳转
-(void)searchAction{
    Mediator *mediator = [Mediator sharedInstance];
    UIViewController *searchController = [mediator BKSearchViewController];
    searchController.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:searchController animated:NO];
}


#pragma mark ------------------请求标题数组-----------------------------
-(void)requestTitleData{
    
    __weak typeof(self) weakSelf = self;
    
    __strong typeof(weakSelf) strongSelf = weakSelf;
    
    if(strongSelf.exceptionView) {
        [strongSelf.exceptionView dismiss];
        strongSelf.exceptionView = nil;
    }
    
    
    
    RequestManager *manager = [RequestManager manager];
    [LoadingView loadingShoppingCartInView:self.view];
    [manager bkDataRequest:RequestMethodGet URLString:[BKMainCommon ServicesURL:@"/indexTopCategory"] parameters:nil success:^(id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            BOOL success = [[responseObject objectForKey:@"success"] boolValue];
            
            if (success) {
               
                NSArray *resultArr = responseObject[@"data"][@"list"];
                NSMutableArray *titleArr = [NSMutableArray array];
                NSMutableArray *idArr = [NSMutableArray array];
                NSMutableArray *picArray = [NSMutableArray array];
                NSMutableArray *selectedArray = [NSMutableArray array];
                for (NSDictionary *dic in resultArr) {
                    BKScrollViewHeaderTitleModel *model = [[BKScrollViewHeaderTitleModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    [titleArr addObject:model.name];
                    [idArr addObject:model.id];
                    [picArray addObject:model.icon1];
                    [selectedArray addObject:model.icon2];
                }
                self.titleArray = titleArr;
                self.categoryArray = idArr;
                self.pictures = picArray;
                self.select_pictures = selectedArray;
                self.fourLayoutView.headerViewDelegate = self;
                [self.view addSubview:self.fourLayoutView];
                [self addView_ScrollHeaderView];
                [LoadingView dismiss];
 
                NSData *data = [self returnDataWithDictionary:responseObject[@"data"]];
                BKFirstPageDataModel *dataModel = [BKFirstPageDataModel getCurrentFPData:FPData_titleType];
                [dataModel.realm beginWriteTransaction];
                dataModel.FPData_title = data;
                [dataModel.realm commitWriteTransaction];
            }else{
                BKFirstPageDataModel *FPData = [BKFirstPageDataModel getCurrentFPData:FPData_titleType];
                NSDictionary *FPData_dic= [self dicWithData:FPData.FPData_first];
                if (FPData_dic.allKeys != 0) {
                    NSArray *resultArr = FPData_dic[@"list"];
                    NSMutableArray *titleArr = [NSMutableArray array];
                    NSMutableArray *idArr = [NSMutableArray array];
                    NSMutableArray *picArray = [NSMutableArray array];
                    NSMutableArray *selectedArray = [NSMutableArray array];
                    for (NSDictionary *dic in resultArr) {
                        BKScrollViewHeaderTitleModel *model = [[BKScrollViewHeaderTitleModel alloc]init];
                        [model setValuesForKeysWithDictionary:dic];
                        [titleArr addObject:model.name];
                        [idArr addObject:model.id];
                        [picArray addObject:model.icon1];
                        [selectedArray addObject:model.icon2];
                    }
                    self.titleArray = titleArr;
                    self.categoryArray = idArr;
                    self.pictures = picArray;
                    self.select_pictures = selectedArray;
                    self.fourLayoutView.headerViewDelegate = self;
                    [self.view addSubview:self.fourLayoutView];
                    [self addView_ScrollHeaderView];

                }else{
                    ExceptionCodeType type = ExceptionCodeTypeServerError;
                    if (iPhone5 || iPhone4s) {
                        weakSelf.exceptionView = [ExceptionView exceptionViewWithType:type controller:self];
                    }else{
                        weakSelf.exceptionView = [ExceptionView exceptionViewWithType:type];
                    }
                    ExceptionViewAction *retryAction = [ExceptionViewAction actionWithType:type handler:^(ExceptionViewAction *action) {
                        [weakSelf requestTitleData];
                    }];
                    weakSelf.exceptionView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
                    [weakSelf.exceptionView addAction:retryAction];
                    [weakSelf.exceptionView showInView:_backView];

                    
                    
                }
                [LoadingView dismiss];

            }
        }

        
        
    } failure:^(id  _Nullable responseObject, NSError * _Nullable error) {
        [LoadingView dismiss];
        BKFirstPageDataModel *data = [BKFirstPageDataModel getCurrentFPData:FPData_titleType];
                    NSDictionary *dataDic =  [self dicWithData:data.FPData_title];
                    if (dataDic == nil) {
                        if (!weakSelf.exceptionView && error) {
                            ExceptionCodeType type = ExceptionCodeTypeNoNetwork;
                            if (error.code == -1009) {
                                type = ExceptionCodeTypeNoNetwork;
                            }else if (error.code == -1001){
                                type = ExceptionCodeTypeRequestFailed;
                            }else if (error.code == 502 || error.code == 404){
                                type = ExceptionCodeTypeServerError;
                            }else{
                                type = ExceptionCodeTypeFailedToLoad;
                            }
                            
                            if (iPhone5 || iPhone4s) {
                                weakSelf.exceptionView = [ExceptionView exceptionViewWithType:type controller:self];
                            }else{
                                weakSelf.exceptionView = [ExceptionView exceptionViewWithType:type];
                            }

                            ExceptionViewAction *retryAction = [ExceptionViewAction actionWithType:type handler:^(ExceptionViewAction *action) {
                                [weakSelf requestTitleData];
                            }];
                            weakSelf.exceptionView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
                            [weakSelf.exceptionView addAction:retryAction];
                            [weakSelf.exceptionView showInView:_backView];

                        }

                    }else{
        
                        NSArray *resultArr = [dataDic objectForKey:@"list"];
                        NSMutableArray *titleArr = [NSMutableArray array];
                        NSMutableArray *idArr = [NSMutableArray array];
                        NSMutableArray *picArray = [NSMutableArray array];
                        NSMutableArray *selectedArray = [NSMutableArray array];
                        for (NSDictionary *dic in resultArr) {
                            BKScrollViewHeaderTitleModel *model = [[BKScrollViewHeaderTitleModel alloc]init];
                            [model setValuesForKeysWithDictionary:dic];
                            [titleArr addObject:model.name];
                            [idArr addObject:model.id];
                            [picArray addObject:model.icon1];
                            [selectedArray addObject:model.icon2];
                        }
                        self.titleArray = titleArr;
                        self.categoryArray = idArr;
                        self.pictures = picArray;
                        self.select_pictures = selectedArray;
                        self.fourLayoutView.headerViewDelegate = self;
                        [self.view addSubview:self.fourLayoutView];
                        [self addView_ScrollHeaderView];

                        
                    }
        
     
    }];
}
#pragma mark -------------------BKScrollPageViewDelegate---------------
- (NSInteger)numberOfChildViewControllers {
    return self.titleArray.count;
}


- (UIViewController<BKScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<BKScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    UIViewController<BKScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    
    if (!childVc) {
        childVc = [[BKFirstPageNewsListViewController alloc] init];
    }
    
    
        SEL selector = NSSelectorFromString(@"passCategoryIdArray:");
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Warc-performSelector-leaks"
    [childVc performSelector:selector withObject:self.categoryArray];
#pragma clang diagnostic pop
    return childVc;
}

- (void)scrollPageController:(UIViewController *)scrollPageController childViewControllDidAppear:(UIViewController *)childViewController forIndex:(NSInteger)index{
 
    self.index = index;
    if (index  == 0) {
        [self.view bringSubviewToFront:self.scrollPageView];
//        self.changeShowTypeBtn.hidden = YES;
        return;
        
    }
    
    if (index < 2 && index > 0) {
        [self.view bringSubviewToFront:self.scrollPageView];
//        self.changeShowTypeBtn.hidden = YES;
    }else if (index>2){
        [self.view bringSubviewToFront:self.fourLayoutView];
//        [self.view bringSubviewToFront:self.changeShowTypeBtn];
//        self.changeShowTypeBtn.hidden = NO;
//        NSDictionary *info_dic = @{@"goodsShowType":[NSString stringWithFormat:@"%ld",(long)self.type_change], @"index":[NSString stringWithFormat:@"%ld",(long)self.index]};
//        
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"only_changeTypeNotication" object:@"goodsShowType" userInfo:info_dic];
    }else{
        [self.view bringSubviewToFront:self.scrollPageView];
//        self.changeShowTypeBtn.hidden = YES;
    }
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}
#pragma mark -------------------添加头部可滚动视图------------------------
-(void)addView_ScrollHeaderView{
    //必要的设置, 如果没有设置可能导致内容显示不正常
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    BKSegmentStyle *style = [[BKSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;

    //设置滚动条颜色
    style.scrollLineColor = RGB(3, 181, 152);
    //标题选中的颜色
    style.selectedTitleColor = RGB(3, 181, 152);
    style.normalTitleColor = RGB(136, 136, 136);
    style.adjustTitleWhenBeginDrag = NO;
    style.scaleTitle = NO;
    style.autoAdjustTitlesWidth = YES;
    style.segmentHeight = 60;
//    style.contentViewBounces = NO;
    //标题之间的间隙
    style.titleMargin = 20;
    style.scrollLineHeight = 2;
    style.titleFont = [Common systemFontOfSize:12];
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    // 初始化
    style.imagePosition = TitleImagePositionTop;
    [style setShowImage:YES];
    self.scrollPageView = [[BKScrollPageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) segmentStyle:style titles:self.titleArray pictures:self.pictures select_pictures:self.select_pictures parentViewController:self delegate:self];
    
    [self.view addSubview:self.scrollPageView];
//    [self.view addSubview:self.changeShowTypeBtn];

}

//-(BKMain_CustomSwitchBtn *)changeShowTypeBtn{
//    if (!_changeShowTypeBtn) {
//        _changeShowTypeBtn = [[BKMain_CustomSwitchBtn alloc]initWithFrame:CGRectZero];
//        _changeShowTypeBtn.hidden = YES;
//        _changeShowTypeBtn.selected = YES;
//        _changeShowTypeBtn.myDelegate = self;
//        [_changeShowTypeBtn setDragEnable:YES];
//        [_changeShowTypeBtn setAdsorbEnable:YES];
//        _changeShowTypeBtn.frame = CGRectMake(ScreenWidth-40, 60 + 5+35, 30, 30);
//        [_changeShowTypeBtn addTarget:self action:@selector(changeShowType:) forControlEvents:UIControlEventTouchUpInside];
//        [_changeShowTypeBtn setBackgroundImage:[UIImage imageNamed:@"商品列表样式1"] forState:UIControlStateNormal];
//        [_changeShowTypeBtn setBackgroundImage:[UIImage imageNamed:@"商品列表样式2"] forState:UIControlStateSelected];
//
//    }
//    return _changeShowTypeBtn;
//}
#pragma mark ----------------更换展示的样式--------------------------------------
//-(void)changeShowType :(UIButton *)btn{
//    
//    if (btn.isSelected) {
//        btn.selected = NO;
//        self.type_change = 1;
//    } else {
//        btn.selected = YES;
//        self.type_change = 2;
//    }
//    
//    NSDictionary *info_dic = @{@"goodsShowType":[NSString stringWithFormat:@"%ld",(long)self.type_change], @"index":[NSString stringWithFormat:@"%ld",(long)self.index]};
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeTypeNotication" object:@"goodsShowType" userInfo:info_dic];
//}
#pragma mark ------------------BKMain_CustomSwitchBtnDelegate-------------------------
//-(void)CurrentLocationOrignalY:(CGFloat)orignalY begainPoint:(CGPoint)point{
//    if (65+35 > orignalY) {
//        [UIView animateWithDuration:0.2 animations:^{
//            self.changeShowTypeBtn.frame = CGRectMake(ScreenWidth-40, 65+35, 30, 30);
//        }];
//    }
//
//}

//判断是否有系统消息
-(void)isHaveNewSystemMessage{
    if ([BKMainCommon islogin]) {
        BKUserStatusInfo *userInfo = [BKUserStatusInfo currentUser];
        if (![userInfo.unreadMessageNum isEqualToString:@""]) {
            self.badgeView.hidden = NO;
        }else{
            self.badgeView.hidden = YES;
        }
    }else{
        self.badgeView.hidden = YES;
    }
    
    
}

//评价弹框
-(void)showEvaluateAPPView{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        if ([BKMainCommon islogin]) {
            [BKMainCommon getAPPStoreEvaluateCallBack:^(id obj, NSError *error) {
                if (obj) {
                    // "flag": true,
                    NSDictionary *dic = obj;
                    if ([dic[@"flag"] boolValue]) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            BKMain_EvaluateAlertView *alert = [[BKMain_EvaluateAlertView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
                            [alert show];
                        });
                    }
                    
                }
            }];
        }else{
            return ;
        }
    });
}

#pragma mark ------------------跳转详情分类-------------------------
//type:1:分类, 2/3:商品详情, 4:h5, 5:大分类如国内商品, 6:需要传st的h5, 7:话费充值和积分签到
//code:对应type的分类id、商品编号、h5地址、大分类code值[1:国内商品，2：全球购，3：有糖自营]， type7的code[4:充话费, 5:签到]
-(void)pushWithType:(NSString*)type code:(NSString*)code name:(NSString*)name saleTypes:(id)saleTypes{
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    
    id object = [self nextResponder];
    while (![object isKindOfClass:[RootNavigationController class]] && object != nil){
        object = [object nextResponder];
    }
    RootNavigationController *navi = (RootNavigationController*)object;
    
    if([[NSString stringWithFormat:@"%@", type] isEqualToString:@"1"]){
        
        Mediator *mediator = [Mediator sharedInstance];
        UIViewController *classifyVC = [mediator HomeIntorecordPushInformationWithFilterTerms:@"-1" categotyId:code channelType:@"" orderKey:@"" currentPage:0 keyWord:@"" order:@""];
        classifyVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:classifyVC animated:YES];
        
    }else if([[NSString stringWithFormat:@"%@", type] isEqualToString:@"2"] || [[NSString stringWithFormat:@"%@", type] isEqualToString:@"3"]) {
        Mediator *mediator = [Mediator sharedInstance];
        UIViewController *detailVC = [mediator BKGD_goodsDetailViewController:code saleTypes:saleTypes];
        detailVC.view.backgroundColor = [UIColor whiteColor];
        [navi pushViewController:detailVC animated:YES];
        
    }else if([[NSString stringWithFormat:@"%@", type] isEqualToString:@"4"]) {
        
        BKMain_WebViewController *h5 =[[BKMain_WebViewController alloc]init];
        h5.webViewTitle = name;
        h5.webViewUrl = code;
        h5.type = type;
        [navi pushViewController:h5 animated:YES];
        
    }else if([[NSString stringWithFormat:@"%@", type] isEqualToString:@"5"]){
        Mediator *mediator = [Mediator sharedInstance];
        UIViewController *classifyVC = [mediator HomeIntorecordPushInformationWithFilterTerms:@"-1" categotyId:@"" channelType:code orderKey:@"" currentPage:0 keyWord:@"" order:@""];
        classifyVC.view.backgroundColor = [UIColor whiteColor];
        classifyVC.hidesBottomBarWhenPushed = YES;
        [navi pushViewController:classifyVC animated:YES];
        
    }else if ([[NSString stringWithFormat:@"%@", type] isEqualToString:@"6"]){
        if (![BKMainCommon islogin]) {
//            self.islogin = NO;
            Mediator *mediator = [Mediator sharedInstance];
            [mediator userGoToLoginFromOrignalController:self finishBlock:^ {
                
            }];
            
        }else{
            
            BKMain_WebViewController *h5 =[[BKMain_WebViewController alloc]init];
            h5.webViewTitle = name;
            h5.webViewUrl = code;
            h5.type = type;
            [navi pushViewController:h5 animated:YES];
            
        }
    }else if ([[NSString stringWithFormat:@"%@", type] isEqualToString:@"7"]){
        
    }
}
//字典转data
-(NSData *)returnDataWithDictionary:(NSDictionary *)dict
{
    NSMutableData * data = [[NSMutableData alloc] init];
    NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:dict forKey:@"talkData"];
    [archiver finishEncoding];
    
    return data;
}
-(NSDictionary *)dicWithData:(NSData *)data{
    
    NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    
    NSDictionary* myDictionary = [unarchiver decodeObjectForKey:@"talkData"];
    
    [unarchiver finishDecoding];
    return myDictionary;
}

-(BKMain_ClassifyKindsHeaderView *)fourLayoutView{
    if (!_fourLayoutView) {
        _fourLayoutView = [[BKMain_ClassifyKindsHeaderView alloc]initWithFrame:CGRectMake(0, 60, SCREENWIDTH, 35)];
        
    }
    return _fourLayoutView;
}

-(void)refreshFirstPageData{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshFirstPageData" object:nil];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"updateType" object:@"indexValue"];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"isRestNotification" object:@"isRest"];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"priceNotication" object:@"priceType"];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"typeNotication" object:@"classifyType"];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"HideNaviSearchNotion" object:@"BK_main_Hide"];
}

@end
